package baiqitun.stupro.project.core.task.impl;

import baiqitun.stupro.common.exception.enums.support.NotFoundExceptionEnum;
import baiqitun.stupro.common.exception.support.NotFoundException;
import baiqitun.stupro.common.workflow.enums.ProcessVariableEnum;
import baiqitun.stupro.common.workflow.task.AbstractBooleanServiceTaskHandler;
import baiqitun.stupro.project.constant.ProjectProcessVariableEnum;
import baiqitun.stupro.project.constant.SysProjectTypeEnum;
import baiqitun.stupro.project.core.document.entity.SysProjectDocument;
import baiqitun.stupro.project.entity.SysProject;
import baiqitun.stupro.project.service.SysProjectService;
import baiqitun.stupro.project.service.SysProjectUserRoleService;
import baiqitun.stupro.security.entity.SysUser;
import baiqitun.stupro.security.service.SysUserService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 处理创建一生一品项目的任务流程
 * @author kedaji
 */
@Service("createSubjectProjectServiceTaskHandler")
public class CreateSubjectProjectServiceTaskHandler extends AbstractBooleanServiceTaskHandler {
    @Autowired
    private SysProjectService sysProjectService;
    @Autowired
    private SysProjectUserRoleService sysProjectUserRoleService;
    @Autowired
    private SysUserService sysUserService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void handleTrue(DelegateExecution execution) {
        HistoricTaskInstance historicTaskInstance = getHistoricTaskInstance(execution);
        // 获取流程变量
        Map<String, Object> variables = historicTaskInstance.getProcessVariables();
        String projectName = (String) variables.get(ProcessVariableEnum.NAME.name());
        String projectDocumentDescription = (String) variables.get(ProcessVariableEnum.DESCRIPTION.name());
        String projectDocType = (String) variables.get(ProjectProcessVariableEnum.DOCUMENT_TYPE.name());
        Integer scope = (Integer) variables.get(ProjectProcessVariableEnum.SCOPE.name());
        String ownerUserId = (String) variables.get(ProcessVariableEnum.OWNER.name());
        SysUser user = sysUserService.getById(ownerUserId);
        if (user == null){
            throw new NotFoundException(NotFoundExceptionEnum.USER_NOT_FOUND);
        }

        // 创建项目
        SysProject project = new SysProject();
        project.setType(SysProjectTypeEnum.SUBJECT.getValue()).setScope(0).setName(projectName).setScope(scope).setCreateBy(user.getUsername());
        SysProjectDocument document = new SysProjectDocument();
        document.setContent(projectDocumentDescription).setType(projectDocType).setCreateBy(user.getUsername());
        sysProjectService.saveWithDocument(project, document);
        sysProjectUserRoleService.saveUserToProject(project.getId(), ownerUserId, user.getUsername());
        //TODO 发送一条信息
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void handleFalse(DelegateExecution execution) {
        //TODO 发送一条信息
    }
}
